using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using _3DTicTacToe.Core;

namespace _3DTicTacToe
{
    partial class GameWindow
    {
        private long _eventCounter;

        private static GeometryDrawing DrawCircle(int x, int y, Color color)
        {
            return new GeometryDrawing
                       {
                           Brush = new SolidColorBrush(color),
                           Pen = new Pen(new SolidColorBrush(Colors.Transparent), 0),
                           Geometry = new RectangleGeometry(new Rect(100*x + 10, 100*y + 10, 80, 80), 40, 40)
                       };
        }

        private static GeometryDrawing DrawRectangle(int x, int y, Color color)
        {
            return new GeometryDrawing
                       {
                           Brush = new SolidColorBrush(color),
                           Pen = new Pen(new SolidColorBrush(Colors.Transparent), 0),
                           Geometry = new RectangleGeometry(new Rect(100*x + 10, 100*y + 10, 80, 80))
                       };
        }

        private static Point GetPosition(Point point, FrameworkElement element)
        {
            var x = (int)Math.Floor(point.X / (element.ActualWidth / 4));
            var y = (int)Math.Floor(point.Y / (element.ActualHeight / 4));

            return new Point(x, y);
        }

        private void ClearPreview()
        {
            Preview1.Children.Clear();
            Preview2.Children.Clear();
            Preview3.Children.Clear(); 
            Preview4.Children.Clear();
            Preview4.Children.Clear();
        }

        void SetCurrentlySelectedText(string text)
        {
            if (text == null)
                currentlySelected.Tag = "0";
            else
            {
                currentlySelected.Content = text;

                if ((string)currentlySelected.Tag == "0")
                    currentlySelected.Tag = "1";
            }
        }

        void DrawPreview(Player player, int x1, int x2, int x3)
        {
            DrawingGroup field = null;
            var radius = 0;

            switch (x3)
            {
                case 0:
                    field = Preview1;
                    break;
                case 1:
                    field = Preview2;
                    break;
                case 2:
                    field = Preview3;
                    break;
                case 3:
                    field = Preview4;
                    break;
            }

            if (field == null) return;

            if (player == _game.PlayerTwo)
                radius = 40;

            field.Children.Add(
                new GeometryDrawing
                    {
                        Brush = new SolidColorBrush(Color.FromArgb((byte) (player.Color.A/2), player.Color.R,
                                                                   player.Color.G, player.Color.B)),
                        Pen = new Pen(new SolidColorBrush(Colors.Transparent), 0),
                        Geometry = new RectangleGeometry(new Rect(100*x1 + 10, 100*x2 + 10, 80, 80),
                                                         radius, radius)
                    });
        }

        private void imgGrid_MouseMove(object sender, MouseEventArgs e)
        {
            if (_game.IsOver) return;

            ++_eventCounter;
            if (_eventCounter % 7 != 0)
                return;
            _eventCounter = 0; // performance

            ClearPreview();

            Image i;
            int z;

            if (Image1.IsMouseOver)
            {
                i = Image1;
                z = 0;
            }
            else if (Image2.IsMouseOver)
            {
                i = Image2;
                z = 1;
            }
            else if (Image3.IsMouseOver)
            {
                i = Image3;
                z = 2;
            }
            else if (Image4.IsMouseOver)
            {
                i = Image4;
                z = 3;
            }
            else
                return;

            var p = GetPosition(e.GetPosition(i), i);
            var t = new Turn(_game.NextPlayer, (int)p.X, (int)p.Y, z);

            SetCurrentlySelectedText(string.Format("({0}|{1}|{2})", t.X1, t.X2, t.X3));

            if (_game.IsValidTurn(t))
            {
                DrawPreview(t.Player, t.X1, t.X2, t.X3);
                DrawPreview3D(t);              
            }
            else
            {
                ClearPreview3D();
                ClearPreview();
            }
        }

        void DrawImageOnTurn(Turn t)
        {
            Action a = () =>
                           {

                               var drawing = (t.Player == _game.PlayerOne)
                                                 ? DrawRectangle(t.X1, t.X2, t.Player.Color)
                                                 : DrawCircle(t.X1, t.X2, t.Player.Color);

                               switch (t.X3)
                               {
                                   case 0:
                                       Field1.Children.Add(drawing);
                                       break;
                                   case 1:
                                       Field2.Children.Add(drawing);
                                       break;
                                   case 2:
                                       Field3.Children.Add(drawing);
                                       break;
                                   case 3:
                                       Field4.Children.Add(drawing);
                                       break;
                               }
                           };
            Dispatcher.Invoke(a, null);
        }

        private void imgGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_game.IsOver) return;

            Image img = null;
            var z = 0;
            if (Image1.IsMouseOver)
            {
                img = Image1;
                z = 0;
            }
            else if (Image2.IsMouseOver)
            {
                img = Image2;
                z = 1;
            }
            else if (Image3.IsMouseOver)
            {
                img = Image3;
                z = 2;
            }
            else if (Image4.IsMouseOver)
            {
                img = Image4;
                z = 3;
            }

            if (img == null) return;

            var p = e.GetPosition(img);
            var x = (int)Math.Floor(p.X / (img.ActualWidth / 4));
            var y = (int)Math.Floor(p.Y / (img.ActualHeight / 4));
            var t = new Turn(_game.NextPlayer, x, y, z);

            if (!_game.IsValidTurn(t)) return;

            ClearPreview3D();
            ClearPreview();

            _game.MakeTurn(t);
        }

        private void imgGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            ClearPreview();
            ClearPreview3D();
            SetCurrentlySelectedText(null);
        }
    }
}